'use strict';

define(['controllers', '../service/statService', './filterController', 'highcharts'], function (controllers) {
    controllers.stat.register('CacheController', ['$scope', '$rootScope', 'StatService', function ($scope, $rootScope, statService) {
        $scope.onlyNode = false;

        $scope.reloadChart = function(divId, title, stat, width) {
            var series = [];
            for (var i = 0; i < stat.series.length; i++) {
                var serie = {
                    name: stat.series[i].name,
                    data: []
                };
                for (var key in stat.series[i].data) {
                    serie.data.push([key - 0, stat.series[i].data[key]]);
                }
                series.push(serie);
            }
            var startTime = stat.categories[0];
            var endTime = stat.categories[stat.categories.length - 1];
            var tickInterval = (new Date(endTime).getDayEndTime() + 1 - startTime) / 12;
            return new Highcharts.Chart({
                chart: {
                    type: 'spline',
                    renderTo: divId,
                    width: width
                },
                title: {
                    text: title,
                    x: -20
                },
                xAxis: {
                    type: 'datetime',
                    labels: {
                        formatter: function () {
                            return $rootScope.formatStatTime(this.value, $scope.param.statType);
                        }
                    }
                },
                plotOptions: {
                    spline: {
                        lineWidth: 1,
                        states: {
                            hover: {
                                lineWidth: 3
                            }
                        },
                        marker: {
                            enabled: false
                        },
                        pointInterval: tickInterval,
                        pointStart: startTime
                    }
                },
                yAxis: {
                    title: {
                        text: '命中率'
                    },
                    min: 0
                },
                tooltip: {
                    formatter: function () {
                        return  '<b>' + this.series.name + '</b><br/>' +
                            $rootScope.formatStatTime(this.x, $scope.param.statType) + '<br/>' +
                            '命中率:' + this.y.toFixed(2);
                    }
                },
                series: series
            });
        };

        $scope.getStat = function (param) {
            $scope.param = param;
            statService.getCacheStat(param, function (stat) {
                //数据图表
                var chartWidth = Math.max($("#chart_panel").width(), $('#list_panel').width());
                if ($scope.chart != null) {
                    $scope.chart.destroy();
                }
                $scope.chart = $scope.reloadChart('chart', '缓存命中率统计', stat, chartWidth);

                //数据列表
                var list = {};
                for (var i = 0; i < stat.series.length; i++) {
                    var serie = stat.series[i];
                    list[serie.name] = {};
                    for (var j = 0; j < stat.categories.length; j++) {
                        var categorie = stat.categories[j];
                        var time = new Date(categorie - 0).format('yyyy-MM-dd HH:mm:ss');
                        list[serie.name][time] = serie.data[categorie];
                    }
                }

                $scope.list = list;
            });
        };
    }]);
});